import 'package:fluplugin/flu_view.dart';
import 'package:fluplugin/fluplugin.dart';
import 'package:flutter/material.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {

  var num = 0;

  @override
  void initState() {
    Fluplugin.setMethodCall(methodCall: (call){

    });
    Fluplugin.setEventMsgCall(msgCall: (msgCall){
      print('--->setEventMsgCall,msgCall=$msgCall');
      if(msgCall!=null&& msgCall is Map){
        num = msgCall['num'];
        setState(() {});
      }
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('HomePage'),
      ),
      body: Column(
        children: [


          Text(
            '我是flutter View，接收原生端传过来的参数为：${num}'
          ),
          MaterialButton(onPressed: ()=>btn(),color: Colors.blue,child: Text(
            '我是flutter button，点击向原生端view传值'
          ),),
          MaterialButton(onPressed: ()=>btn2(),color: Colors.blue,child: Text(
            '我是flutter button，点击向原生端传递消息'
          ),),
          SizedBox(height: 60,),
          SizedBox(
            height: 300,
            width: MediaQuery.of(context).size.width,
            child: FluView(
              key: fluViewStateKey,
            ),
          ),
        ],
      ),
    );
  }

  btn() {
    num ++ ;
    fluViewStateKey.currentState?.addNum(num);
  }

  btn2() {
    Fluplugin.postMsg({'num': num});
  }
}
